import { ExclamationCircleFilled } from '@ant-design/icons'
import { ActionType } from '@ant-design/pro-table';
import type { ProColumns } from '@ant-design/pro-components';
import { Button, Modal, Tag, message } from 'antd';
import type { ZYBLItem } from '@/interfaces/bl'
import deleteConfirm from '@/utils/deleteConfirm'
import { deleteRecord, cy } from '@/services/zy'
import WithAuthComp from '@/components/WithAuthComp';
import { ROLE } from '@/common/const'
const { confirm } = Modal;

const showLeaveHospitalConfirm = ({ id, action }) => {
    confirm({
        title: '确认出院?',
        width: 450,
        icon: <ExclamationCircleFilled />,
        content: '出院后病历将无法修改，请确认是否要执行出院操作？',
        okText: '确认',
        cancelText: '取消',
        onOk() {
            return new Promise(async (resolve, reject) => {
                try {
                    await cy(id)
                    message.success('出院成功！')
                    resolve(true);
                    action?.reload(true);
                } catch (error) {

                }
            })
        },
        onCancel() {
            console.log('Cancel');
        },
    });
};

const getColumns: (props) => ProColumns<ZYBLItem>[] = (props) => {
    const { addMedicalRef, fileAddRef, medicalDetailRef, setId, setEditable } = props
    return [
        {
            title: '门诊号',
            dataIndex: 'recordId',
        },
        {
            title: '胸牌号',
            dataIndex: 'patientnumber',
        },
        {
            title: '姓名',
            dataIndex: 'name',
        },
        {
            title: '监区',
            dataIndex: 'jq',
        },
        {
            title: '住院时间',
            key: 'showTime',
            dataIndex: 'zyDate',
            valueType: 'date',
            sorter: true,
            hideInSearch: true,
        },
        {
            title: '住院时间',
            dataIndex: 'zyDate',
            valueType: 'dateRange',
            hideInTable: true,
            search: {
                transform: (value) => {
                    return {
                        zyStartTime: value[0],
                        zyEndTime: value[1],
                    };
                },
            },
        },
        {
            title: '住院状态',
            key: 'showTime',
            dataIndex: 'zyStatus',
            valueType: 'date',
            hideInSearch: true,
            render: (_, record) => {
                const isZY = record.zyStatus === '住院'
                return (<Tag color={isZY ? '#fa541c' : '#a0d911'} key={record.zyStatus}>
                    {isZY ? '住院' : '出院'}
                </Tag>)
            },
        },
        {
            title: '住院状态',
            dataIndex: 'zyStatus',
            valueType: 'select',
            valueEnum: {
                '1': {
                    text: '住院'
                },
                '2': {
                    text: '出院'
                }
            },
            hideInTable: true,
        },
        {
            title: '出院日期',
            key: 'cyDate',
            dataIndex: 'cyDate',
            valueType: 'date',
            sorter: true,
            hideInSearch: true,
        },
        {
            title: '出院日期',
            dataIndex: 'cyDate',
            valueType: 'dateRange',
            hideInTable: true,
            search: {
                transform: (value) => {
                    return {
                        cyStartTime: value[0],
                        cyEndTime: value[1],
                    };
                },
            },
        },
        {
            title: '科室',
            dataIndex: 'ks',
            valueType: 'select',
            valueEnum: {
                内科: {
                    text: '内科',
                },
                外科: {
                    text: '外科',
                },
                传染病: {
                    text: '传染病',
                },
            },
        },
        {
            title: '伤种',
            dataIndex: 'sz',
            valueType: 'select',
            valueEnum: {
                // 工伤 他伤 意外伤 自伤自残
                工伤: {
                    text: '工伤',
                },
                他伤: {
                    text: '他伤',
                },
                意外伤: {
                    text: '意外伤',
                },
                自伤自残: {
                    text: '自伤自残',
                },
            },
        },
        {
            title: '操作',
            valueType: 'option',
            key: 'option',
            render: (text, record, _, action) => {
                const isCY = record.zyStatus === '出院';
                return [
                    <WithAuthComp authKey={[ROLE.YS, ROLE.HS]}>
                        <Button
                            size='small'
                            key="add"
                            type='primary'
                            onClick={() => {
                                // setReadonly(false)
                                setId(record.recordId)
                                addMedicalRef?.current?.showModelRef();
                            }}
                            disabled={isCY}
                        >
                            新增病历
                        </Button>
                    </WithAuthComp>
                    ,
                    <WithAuthComp authKey={[ROLE.YS, ROLE.HS, ROLE.ZL]}>
                        <Button
                            color="cyan"
                            size='small'
                            type='primary'
                            key="detail"
                            onClick={() => {
                                setEditable(!isCY)
                                setId(record.recordId)
                                medicalDetailRef?.current?.showModelRef();
                            }}
                        >
                            详情
                        </Button>
                    </WithAuthComp>
                    ,
                    <WithAuthComp authKey={[ROLE.YS, ROLE.HS]}>
                        <Button
                            size='small'
                            type='primary'
                            key="result"
                            ghost
                            disabled={isCY}
                            onClick={() => {
                                // setReadonly(false)
                                setId(record.recordId)
                                fileAddRef?.current?.showModelRef();
                            }}
                        >
                            检验结果
                        </Button>
                    </WithAuthComp>,
                    <WithAuthComp authKey={[ROLE.YS, ROLE.HS]}>
                        {isCY ? null : <Button
                            size='small'
                            type='primary'
                            style={{ background: '#52a41c' }}
                            onClick={() => {
                                showLeaveHospitalConfirm({ id: record.recordId, action })
                            }}
                        >
                            出院
                        </Button>}
                    </WithAuthComp>,
                    <WithAuthComp authKey={[ROLE.ADMIN]}>
                        <Button
                            size='small'
                            type="primary"
                            key="delete"
                            danger
                            onClick={() => {
                                deleteConfirm({ id: record.recordId, action: action as ActionType, deleteApi: deleteRecord })
                            }}
                        >
                            删除
                        </Button>
                    </WithAuthComp>
                ]
            }

        },
    ];
}

export default getColumns;